﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VeMayBayBUS;
using VeMayBayDTO;

namespace QLVMBProject
{
    public partial class ctrNhanLichChuyenBay : UserControl
    {
        // bien toan cuc

        // colum trong datagridview
        DataGridViewTextBoxColumn clSTT = new DataGridViewTextBoxColumn();
        DataGridViewComboBoxColumn clSBTG = new DataGridViewComboBoxColumn();
        DataGridViewTextBoxColumn clTGDung = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn clGhiChu = new DataGridViewTextBoxColumn();      
        public ctrNhanLichChuyenBay()
        {
            InitializeComponent();

            //columns STT
          
            clSTT.Name = "clSTT";
            clSTT.HeaderText = "Số Thứ Tự";
      
            this.dgvSBTrungGian.Columns.Add(clSTT);
            //columns SBTrungGian
            clSBTG.Name = "clSBTG";
            clSBTG.HeaderText = "Sân Bay Trung Gian";
            clSBTG.DataSource = SanBayBUS.GetSanBay();
            clSBTG.ValueMember = "masanbay";
            clSBTG.DisplayMember = "tensanbay";
            this.dgvSBTrungGian.Columns.Add(clSBTG);
            // column Thoigiandung
             clTGDung.Name = "clTGDung";
            clTGDung.HeaderText = "Thời Gian Dừng";
            this.dgvSBTrungGian.Columns.Add(clTGDung);
            //cls ghi chu 
            
            clGhiChu.Name = "clGhiChu";
            clGhiChu.HeaderText = "Ghi Chú";
            this.dgvSBTrungGian.Columns.Add(clGhiChu);


            // sb den
            cbSBDen.DataSource = SanBayBUS.GetSanBay();
            cbSBDen.ValueMember = "masanbay";
            cbSBDen.DisplayMember = "tensanbay";
            // sb di
            cbSBDi.DataSource = SanBayBUS.GetSanBay();
            cbSBDi.ValueMember = "masanbay";
            cbSBDi.DisplayMember = "tensanbay";

            // may bay
            cbMayBay.DropDownStyle = ComboBoxStyle.DropDownList;
            cbMayBay.DataSource = MayBayBUS.GetMayBay();
            cbMayBay.ValueMember = "mamaybay";
            cbMayBay.DisplayMember = "tenmaybay";


        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {

                // get Tham So
                ThamSoDTO ts = new ThamSoDTO();
                ts = ThamSoBUS.GetAllThamSo();


                
                // Khoi tao Chuyen Bay
                ChuyenBayDTO cb = new ChuyenBayDTO();
                cb.MayBay = new MayBayDTO();
                cb.HanhTrinh = new HanhTrinhDTO();
               
                cb.TenChuyenBay = txtTenCB.Text;
                cb.MayBay.MaMayBay= (int)cbMayBay.SelectedValue ;
                //set hanh trinh
                cb.HanhTrinh.SanBayDen = (int)cbSBDen.SelectedValue;
                cb.HanhTrinh.SanBayDi = (int)cbSBDi.SelectedValue;
                cb.HanhTrinh = HanhTrinhBUS.GetHanhTrinh(cb.HanhTrinh);
                //
                //set  value cho ngay gio bay
                int dd = dtNgayDi.Value.Day;
                int MM = dtNgayDi.Value.Month;
                int yyyy = dtNgayDi.Value.Year;
                int hh = (int)nbGio.Value;
                int mm = (int)nbPhut.Value;
                dtNgayDi.Value = new DateTime(yyyy, MM, dd, hh, mm,0);
                //
                cb.NgayGio = dtNgayDi.Value;
                cb.ThoiGianBay = (int)nbThoiGianBay.Value;


                int tgDung = 0;
                    int i=0;
                    if (dgvSBTrungGian.Rows.Count - 1 > ts.SoSanBayTGToiDa)
                    {
                        MessageBox.Show("San Bay Trung Gian Phai It hon hay bang " + ts.SoSanBayTGToiDa.ToString() + "");
                        return;
                    }
                for (i =0 ;i<ts.SoSanBayTGToiDa ;i++)
                {
                    DataGridViewRow row = dgvSBTrungGian.Rows[i];
                    try
                    {

                        tgDung = Convert.ToInt32(row.Cells["clTGDung"].Value);
                        if (tgDung > ts.ThoiGianDungToiDa )
                        {
                            MessageBox.Show("Thoi Gian Dung Phai Nho Hon " + ts.ThoiGianDungToiDa + " phut va Lon Hon " + ts.ThoiGianDungToiThieu + " phut");
                            return;
                        }else if(tgDung <ts.ThoiGianDungToiThieu){
                            MessageBox.Show("Thoi Gian Dung Phai Nho Hon " + ts.ThoiGianDungToiDa + " phut va Lon Hon " + ts.ThoiGianDungToiThieu + " phut");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                        throw;
                    }
                    
                }
              

                // insert lich bay
                bool result = ChuyenBayBUS.NhanLich(cb);
                ChuyenBayDTO lastCB = new ChuyenBayDTO();
                lastCB = ChuyenBayBUS.GetLasChuyenBay();
                // Khoi Tao San Bay Trung Gian
                SanBayTrungGianDTO sbtg = new SanBayTrungGianDTO();
                sbtg.SanBay = new SanBayDTO();
                sbtg.ChuyenBay = new ChuyenBayDTO();
            
                try
                {
                    for (i = 0; i < dgvSBTrungGian.Rows.Count - 1; i++)
                    {
                        sbtg.ChuyenBay.MaChuyenBay = lastCB.MaChuyenBay;
                        sbtg.SanBay.MaSanBay = Convert.ToInt32(dgvSBTrungGian.Rows[i].Cells["clSBTG"].Value);
                        sbtg.ThoiGianDung = Convert.ToInt32(dgvSBTrungGian.Rows[i].Cells["clTGDung"].Value);
                        
                        // insert sb trung gian
                        SanBayTrungGianBUS.InsertSBTrungGian(sbtg);
                    }
                    result = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw;
                }

                //test
                DataTable dt = new DataTable();
                dt = SanBayTrungGianBUS.GetAllSanBayTG();
                if (result)
                {
                    MessageBox.Show("Nhan Lich Bay Thanh Cong");
                }
                // insert san bay trung gian

               
               
            
            }
            catch (Exception ex)
            {


                MessageBox.Show(ex.Message);
                
                throw;
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Remove(this);
        }

      
       


    }
}
